import basicSprite
import pygame
from helpers import *
from textgameprog import *
from math import *

ZEROARR = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
STEP = 1
COS = 0
POLY = 1
MIX = 2
class Spell(basicSprite.Sprite):
    def __init__(self, centerPoint, image=None, caster=None, coscoeff=0, polycoeffs = [0], type = POLY, reverse = False):
        if image == None:
            image, self.rect = load_image('bolt-PH.png',-1)
        basicSprite.Sprite.__init__(self, centerPoint, image)
        self.hit = "false"
        self.hitted = "false"
        self.dervarr = ZEROARR
        self.spell_positions = []
        self.spell_pos = 0
        self.coscoeff = coscoeff
        self.polycoeffs = polycoeffs
        self.type = type # polynomial by default
        self.reverse = reverse# true if spellcaster is on the right side of the screen, usually
        if caster == None:
            self.caster == caster
    
    def update(self):
        self.spell_pos += 1
        if self.spell_pos < len(self.spell_positions):
            self.rect.center = self.spell_positions[self.spell_pos]
        else:
            self.miss()
    
    def miss(self):
        print "Missed"
        self.kill()

    def collide(self):
        self.kill()

    def calculate_spell_positions(self, width=640, height=480):
        if self.type == COS:
            func = self.create_func_cos(self.coscoeff)
            x_scale = 2 * pi
            y_scale = 10
        elif self.type == POLY:
            func = self.create_func_poly(self.polycoeffs)
            x_scale = .1
            y_scale = .05
        elif self.type == "mix":
            func = self.create_func_mix(self.polycoeffs, self.coscoeff)
            x_scale = 1
            y_scale = .05
        else:
            error, "bad function type" 
        spell_positions = self.calculate_path(x_scale, y_scale, width, height, func) # list of tuples
        if self.reverse:
            spell_positions.reverse()
        self.spell_positions = spell_positions
        return spell_positions
    
    def create_func_poly(self, polycoeffs):
        arrnew = []
        for i in range(0, len(polycoeffs)):
            arrnew.append(i)
        return(lambda x: sum(map(self.mult, map(self.exp, [x] * len(polycoeffs), arrnew), polycoeffs)))

    def create_func_cos(self, coscoeff):
       return(lambda x: coscoeff*cos(x))

    def create_func_mix(self, polycoeffs, coscoeff):
        cospart = self.create_func_cos(coscoeff)
        polypart = self.create_func_poly(polycoeffs)
        return(lambda x: cospart(x) * polypart(x))

    def mult(self, x, y):
        return x * y

    def exp(self, x, y):
        return x ** y
    
    
    def calculate_path(self, x_scale, y_scale, width, height, func):

        points = []
        for x in range(0, width):
            y = height/2 - y_scale * func((x-width/2)/x_scale)
            points.append((x,y))
        return points

    """    
def draw_trail(self, spell, position, surface, spell_positions):
        index = spell_positions.index(position) - spell.rect.width # want it offset behind the sprite
        if index >= 0:
            rect = (spell_positions[index], (1,1))
            pygame.draw.rect(surface, (255,255,255), rect)
            return [rect]
        return []
    """
    
         
    def hitabolt(self, wizzard_group):
        #IM ON A BOLT MAN
        if pygame.sprite.spritecollideany(self, wizzard_group):
            print "ack! Ahm HIT!!!"
            self.hit = "True"
        
